package com.meiyu.live.ui.main;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.android.volley.Response;
import com.meiyu.live.BaseActivity;
import com.meiyu.live.BaseApplication;
import com.meiyu.live.R;
import com.meiyu.live.comm.Constant;
import com.meiyu.live.comm.UserConfig;
import com.meiyu.live.net.param.LoginParams;
import com.meiyu.live.net.param.ThirdLoginParams;
import com.meiyu.live.net.response.LoginResponse;
import com.meiyu.live.popj.UrlParams;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by zbl on 2016/8/23.
 */
public class LauncherActivity extends BaseActivity implements View.OnClickListener {

    private static final long DELAY_TIME = 2000;
    private long timePoint = 0;

    private View btn_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);
        init();

        Uri uri = getIntent().getData();
        if (uri != null && !TextUtils.isEmpty(uri.getQueryParameter("params"))) {
            UrlParams params = BaseApplication.mGson.fromJson(uri.getQueryParameter("params"), UrlParams.class);
            if (params.getType() == 1) {
                UserConfig.getInstance().setParams(params);
            }
        }

        if (TextUtils.isEmpty(UserConfig.getInstance().getLogingAccount()) || TextUtils.isEmpty(UserConfig.getInstance().getLogingPassword())) {
            if (TextUtils.isEmpty(UserConfig.getInstance().getOpenId())) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (UserConfig.getInstance().getParams()==null)
                        {
                            startActivity(MainActivity.class);
                        }
                        finish();
                    }
                }, DELAY_TIME);
            } else {
                getDataThird();
            }
        } else {
            getData();
        }
    }

    private void init() {
//        btn_back = findViewById(R.id.btn_back);
//        btn_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
//        if (view == btn_back) {
//            finish();
//        }
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void onError() {
        super.onError();
        long currentTime = System.currentTimeMillis();
        if (currentTime - timePoint < DELAY_TIME) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    startActivity(MainActivity.class);
                    finish();
                }
            }, DELAY_TIME - (currentTime - timePoint));
        } else {
            startActivity(MainActivity.class);
            finish();
        }
    }

    /**
     * 登录
     */
    private void getData() {
        timePoint = System.currentTimeMillis();
        final long accountNumber = Long.parseLong(UserConfig.getInstance().getLogingAccount());
        request(new LoginParams(accountNumber, UserConfig.getInstance().getLogingPassword(), JPushInterface.getRegistrationID(getApplicationContext())), LoginResponse.class, new Response.Listener<LoginResponse>() {
            @Override
            public void onResponse(LoginResponse loginResponse) {
                if (loginResponse != null && loginResponse.getStatusCode() == Constant.StatusCode_Success) {
                    UserConfig.getInstance().setSession_token(loginResponse.getSession_token());
                    UserConfig.getInstance().setNet_ease(loginResponse.getNet_ease());
                    UserConfig.getInstance().setNet_token(loginResponse.getNet_token());
                    UserConfig.getInstance().setUserId(accountNumber);
                    Log.e("UserConfig", "setNet_token: " + loginResponse.getNet_token());
                }

                long currentTime = System.currentTimeMillis();
                if (currentTime - timePoint < DELAY_TIME) {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            startActivity(MainActivity.class);
                            finish();
                        }
                    }, DELAY_TIME - (currentTime - timePoint));
                } else {
                    startActivity(MainActivity.class);
                    finish();
                }
            }
        });
    }

    private void getDataThird() {
        timePoint = System.currentTimeMillis();
        final String third_id = UserConfig.getInstance().getOpenId();
        final String platform = UserConfig.getInstance().getThirdPlatform();
        final String name = UserConfig.getInstance().getThirdName();
        final String avatar = UserConfig.getInstance().getThirdAvatar();
        final String sex = UserConfig.getInstance().getSex();
        request(new ThirdLoginParams(third_id, platform, name, sex, avatar, JPushInterface.getUdid(this), UserConfig.getInstance().getUid()), LoginResponse.class, new Response.Listener<LoginResponse>() {
            @Override
            public void onResponse(LoginResponse loginResponse) {
                if (loginResponse != null && loginResponse.getStatusCode() == Constant.StatusCode_Success) {
                    UserConfig.getInstance().setSession_token(loginResponse.getSession_token());
                    UserConfig.getInstance().setNet_ease(loginResponse.getNet_ease());
                    UserConfig.getInstance().setNet_token(loginResponse.getNet_token());

                    UserConfig.getInstance().setOpenId(third_id);
                }

                long currentTime = System.currentTimeMillis();
                if (currentTime - timePoint < DELAY_TIME) {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            startActivity(MainActivity.class);
                            finish();
                        }
                    }, DELAY_TIME - (currentTime - timePoint));
                } else {
                    startActivity(MainActivity.class);
                    finish();
                }
            }
        });
        Log.e("deviceId", JPushInterface.getUdid(this));
    }

}
